//
//  ViewController.m
//  ComponentBase
//
//  Created by GuoMs on 2018/9/4.
//  Copyright © 2018年 gms. All rights reserved.
//

#import "ViewController.h"
// MediatorKit 需要作为一个库组件都需要依赖它，这里先放base里面
#import "CTMediator+Target_A.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.textLabel.text = self.dataArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = indexPath.row;
    
    if (row == 0) {
        UIViewController *controller =  [[CTMediator sharedInstance] ctm_componentAControllerWithTitle:@"ComponentA"];
        if (controller) {
            [self.navigationController pushViewController:controller animated:YES];
        }
    } else if (row == 1) {
        [[CTMediator sharedInstance]ctm_doSomethingAsyncComplete:^(BOOL isSuccess) {
            [[[UIAlertView alloc]initWithTitle:@"" message:@"回调成功" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]show];
        }];
    } else if (row == 2) {
        
        NSString *str = [[CTMediator sharedInstance]ctm_getString];
        [[[UIAlertView alloc]initWithTitle:@"获取成功" message:str delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]show];
        
    }
    
}


- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
}

- (NSArray *)dataArr {
    
    if (!_dataArr) {
        _dataArr = @[@"跳转到A组件首页",@"异步操作",@"获取值"];
    }
    return _dataArr;
}


@end
